package com.sunyard.inser.generator;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.internal.util.StringUtility;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by jack on 2020/3/1.
 */
public class MyBatisPlugin extends PluginAdapter {
    @Override
    public boolean validate(List<String> list) {
        return true;
    }


    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {

        topLevelClass.addJavaDocLine("/**");

        String remarks = introspectedTable.getRemarks();
        if (StringUtility.stringHasValue(remarks)) {
            String[] remarkLines = remarks.split(System.getProperty("line.separator"));
            for (String remarkLine : remarkLines) {
                topLevelClass.addJavaDocLine(" * " + remarkLine);
            }
        }

        StringBuilder sb = new StringBuilder();
        sb.append(" * ").append(introspectedTable.getFullyQualifiedTable());
        topLevelClass.addJavaDocLine(sb.toString());
        sb.setLength(0);
//        sb.append(" * @author ").append(System.getProperties().getProperty("user.name"));
        sb.append(" * @author ").append("lzhang");
        topLevelClass.addJavaDocLine(sb.toString());
        sb.setLength(0);
        sb.append(" * @date ");
        sb.append(getDateString());
        topLevelClass.addJavaDocLine(sb.toString());
        topLevelClass.addJavaDocLine(" */");
        return true;
    }

    @Override
    public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
                                       IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        field.addJavaDocLine("/**");
        String remarks = introspectedColumn.getRemarks();
        if (StringUtility.stringHasValue(remarks)) {
            String[] remarkLines = remarks.split(System.getProperty("line.separator"));
            for (String remarkLine : remarkLines) {
                field.addJavaDocLine(" * " + remarkLine);
            }
        }
        field.addJavaDocLine(" */");
        return true;
    }




    public boolean clientDeleteByPrimaryKeyMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        method.addJavaDocLine("/**");
        String remarks = "根据主键删除方法";
        if (StringUtility.stringHasValue(remarks)) {
            String[] remarkLines = remarks.split(System.getProperty("line.separator"));
            for (String remarkLine : remarkLines) {
                method.addJavaDocLine(" * " + remarkLine);
            }
            appendComment(method);
        }
        method.addJavaDocLine(" */");
        return true;
    }

    public boolean clientSelectByPrimaryKeyMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        method.addJavaDocLine("/**");
        String remarks = "根据主键查询方法";
        if (StringUtility.stringHasValue(remarks)) {
            String[] remarkLines = remarks.split(System.getProperty("line.separator"));
            for (String remarkLine : remarkLines) {
                method.addJavaDocLine(" * " + remarkLine);
            }
            appendComment(method);
        }
        method.addJavaDocLine(" */");
        return true;
    }

    public boolean clientUpdateByPrimaryKeySelectiveMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        method.addJavaDocLine("/**");
        String remarks = "查询方法";
        if (StringUtility.stringHasValue(remarks)) {
            String[] remarkLines = remarks.split(System.getProperty("line.separator"));
            for (String remarkLine : remarkLines) {
                method.addJavaDocLine(" * " + remarkLine);
            }
            appendComment(method);
        }
        method.addJavaDocLine(" */");
        return true;
    }

    public boolean clientUpdateByPrimaryKeyWithoutBLOBsMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        method.addJavaDocLine("/**");
        String remarks = "主键更新方法";
        if (StringUtility.stringHasValue(remarks)) {
            String[] remarkLines = remarks.split(System.getProperty("line.separator"));
            for (String remarkLine : remarkLines) {
                method.addJavaDocLine(" * " + remarkLine);
            }
            appendComment(method);
        }
        method.addJavaDocLine(" */");
        return true;
    }

    public boolean clientUpdateByPrimaryKeySelectiveMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        method.addJavaDocLine("/**");
        String remarks = "条件更新方法";
        if (StringUtility.stringHasValue(remarks)) {
            String[] remarkLines = remarks.split(System.getProperty("line.separator"));
            for (String remarkLine : remarkLines) {
                method.addJavaDocLine(" * " + remarkLine);
            }
            appendComment(method);
        }
        method.addJavaDocLine(" */");
        return true;
    }

    public boolean clientInsertMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        method.addJavaDocLine("/**");
        String remarks = "新增方法";
        if (StringUtility.stringHasValue(remarks)) {
            String[] remarkLines = remarks.split(System.getProperty("line.separator"));
            for (String remarkLine : remarkLines) {
                method.addJavaDocLine(" * " + remarkLine);
            }
            appendComment(method);
        }
        method.addJavaDocLine(" */");
        return true;
}

    public boolean clientInsertSelectiveMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        method.addJavaDocLine("/**");
        String remarks = "可选字段新增方法";
        if (StringUtility.stringHasValue(remarks)) {
            String[] remarkLines = remarks.split(System.getProperty("line.separator"));
            for (String remarkLine : remarkLines) {
                method.addJavaDocLine(" * " + remarkLine);
            }
            appendComment(method);
        }
        method.addJavaDocLine(" */");
        return true;
    }


    private void appendComment(Method method)
    {
        System.out.println("接口名称:" +method.getName());
        List<Parameter> parameterList = method.getParameters();
        System.out.println(parameterList);
        if (parameterList != null)
        {
            for (Parameter parameter : parameterList)
            {
                method.addJavaDocLine(" * @param " + parameter.getName());
            }
        }

        System.out.println( method.getReturnType());
        if (null != method.getReturnType())
        {
            method.addJavaDocLine(" * @return " + method.getReturnType());
        }
    }







    @Override
    public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {

        interfaze.addJavaDocLine("/**");

        String remarks = introspectedTable.getRemarks().replace("表", "接口");
        if (StringUtility.stringHasValue(remarks)) {
            String[] remarkLines = remarks.split(System.getProperty("line.separator"));
            for (String remarkLine : remarkLines) {
                interfaze.addJavaDocLine(" * " + remarkLine);
            }
        }

        StringBuilder sb = new StringBuilder();
        sb.append(" * ").append(introspectedTable.getFullyQualifiedTable());
        interfaze.addJavaDocLine(sb.toString());
        sb.setLength(0);
//        sb.append(" * @author ").append(System.getProperties().getProperty("user.name"));
        sb.append(" * @author ").append("lzhang");
        interfaze.addJavaDocLine(sb.toString());
        sb.setLength(0);
        sb.append(" * @date ");
        sb.append(getDateString());
        interfaze.addJavaDocLine(sb.toString());
        interfaze.addJavaDocLine(" */");

        return true;
    }

    @Override
    public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        //不生成getter
        return true;
    }

    @Override
    public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        //不生成setter
        return true;
    }

    protected String getDateString() {

        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }

}
